package com.ww.springboot.boot.algorithm.leetcode1;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-04-02 09:13
 */
public class BH420强密码校验器 {


    public static void main(String[] args) {
        System.out.println(strongPasswordChecker("1337C0d3"));
    }

    /**
     * 1.遍历字符串 分别获取长度,类型,连续字符串的情况
     *
     * @param password
     * @return
     */
    public static int strongPasswordChecker(String password) {
        int length = password.length();
        Boolean lowerCase = false;
        Boolean upperCase = false;
        Boolean numCase = false;

        char ch1 = '?';
        int total = 1;
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            char ch = password.charAt(i);
            if (Character.isUpperCase(ch)) {
                upperCase = true;
            }
            if (Character.isLowerCase(ch)) {
                lowerCase = true;
            }
            if (Character.isDigit(ch)) {
                numCase = true;
            }
            if (ch1 == '?') {
                ch1 = ch;
            } else if (ch1 == ch) {
                total++;
            } else {
                if (total > 3) {
                    list.add(total);
                }
                ch1 = ch;
                total = 1;
            }
            if (i == length - 1) {
                if (total >= 3) {
                    list.add(total);
                }
            }
        }
        int type = 3 - (lowerCase ? 1 : 0) - (upperCase ? 1 : 0) - (numCase ? 1 : 0);
        int times = 0;
        if (length < 6) {
            //小于6 则至少要插入6-length 次
            int times1;
            if (list.contains(5)) {
                times1 = 2;
            } else if (list.size() != 0) {
                times1 = 1;
            } else {
                times1 = 0;
            }
            int times2 = Math.max(type, 6 - length);

            times = Math.max(times1, times2);
        } else if (length > 20) {
            //
            int times2 = length - 20;
            //分别统计使用减法 使用加法 使用修改需要的次数
            int timeA = 0;
            int timeB = 0;
            int timeC = 0;
            for (int i = 0; i < list.size(); i++) {
                //优先使用减法消除重复
                int num = list.get(i);
                timeA += num - 2;
                timeA += num / 2 + (num % 2 == 0 ? 0 : 1);
                timeA += num / 3;
            }
            times = Math.max(2*Math.max(timeA,type)+times2,Math.max(timeB,type)+times2);
            times = Math.max(times,Math.max(Math.max(timeC,times2),type));
        } else {


        }
        return times;
    }
}
